//
//  MSForgetController.m
//  觅尚
//
//  Created by No.8 on 2019/3/20.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSForgetController.h"
#import "MSForgetView.h"

@interface MSForgetController ()

/** h界面view*/
@property(nonatomic,strong)MSForgetView *ForgetView;

/** id*/
@property(nonatomic,strong)NSString *msgId;


@end

@implementation MSForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self AddLoginView];
    
    [self AnimationClick];
}

//添加界面
- (void)AddLoginView{
    
    self.ForgetView = [[MSForgetView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.ForgetView];
    
   
    
    self.ForgetView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"注册bg"]];
    UIView * ImageView = [[UIView alloc] initWithFrame:self.ForgetView.frame];
    UIImage * image = [UIImage imageNamed:@"注册bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [self.ForgetView addSubview:ImageView];
    
    
    [self.ForgetView addSubview:self.ForgetView.backButton];
    
    [self.ForgetView addSubview:self.ForgetView.logintitle];
    
    [self.ForgetView addSubview:self.ForgetView.iphoneTextField];
    
    [self.ForgetView addSubview:self.ForgetView.iphonelineIMG];
    
    [self.ForgetView addSubview:self.ForgetView.codeTextField];
    
    [self.ForgetView addSubview:self.ForgetView.codeButton];
    
    [self.ForgetView addSubview:self.ForgetView.codelineIMG];
    
    [self.ForgetView addSubview:self.ForgetView.newpassworldTextField];
    
    [self.ForgetView addSubview:self.ForgetView.newvisibleButton];
    
    [self.ForgetView addSubview:self.ForgetView.newpassworldlineIMG];
    
    [self.ForgetView addSubview:self.ForgetView.confirmpassworldTextField];
    
    [self.ForgetView addSubview:self.ForgetView.confirmlineIMG];
    
    [self.ForgetView addSubview:self.ForgetView.confirmvisibleButton];
    
    [self.ForgetView addSubview:self.ForgetView.loginButton];
    
    
}
//添加事件
- (void)AnimationClick{
   // 完成按钮
    [self.ForgetView.loginButton addTarget:self action:@selector(AnimationloginClick) forControlEvents:UIControlEventTouchUpInside];

    //获取验证码按钮
    [self.ForgetView.codeButton addTarget:self action:@selector(AnimationcodeButtonClick) forControlEvents:UIControlEventTouchUpInside];


    //新密码密码可见按钮
    [self.ForgetView.newvisibleButton addTarget:self action:@selector(AnimationnewvisibleButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    //确认密码按钮
    [self.ForgetView.confirmvisibleButton addTarget:self action:@selector(AnimationconfirmvisibleButtonButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //返回
     [self.ForgetView.backButton addTarget:self action:@selector(AnimationbackButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}
/**
 *  返回
 */
- (void)AnimationbackButtonClick{
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

/**
 * 点击完成按钮
 */
- (void)AnimationloginClick{
    WYCLogFunc;
    [MBManager showLoading];
    NSString *iphoneTextStr = [self.ForgetView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *newpassworldTextStr = [self.ForgetView.newpassworldTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *codeTextStr = [self.ForgetView.codeTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    
    NSString *confirmTextStr = [self.ForgetView.confirmpassworldTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (iphoneTextStr.length == 0){
        
        [MBManager showBriefAlert:@"手机号不能为空"];
        return;
    }
    if (newpassworldTextStr.length == 0){
        
        [MBManager showBriefAlert:@"密码不能为空"];
        return;
    }
    if (codeTextStr.length == 0){
        
        [MBManager showBriefAlert:@"验证码不能为空"];
        return;
    }
    if (![confirmTextStr isEqualToString:newpassworldTextStr]){
        
        [MBManager showBriefAlert:@"两次密码不一致"];
        return;
    }
    NSDictionary *params = @{@"phoneNumber":iphoneTextStr,@"passWord":newpassworldTextStr,@"msgId":self.msgId,@"code":codeTextStr};
    [HTTPRequest POST:MSupdatePassword params:params succeed:^(id responseObject) {
        if (codeTextStr){
            [MBManager hideAlert];
            [MBManager showBriefAlert:@"修改成功"];
            [self dismissViewControllerAnimated:NO completion:nil];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
            [MBManager hideAlert];
    }];
}

/**
 * 获取验证码按钮
 */
- (void)AnimationcodeButtonClick{
    WYCLogFunc;
    NSString *iphoneTextStr = [self.ForgetView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (iphoneTextStr.length != 11) {
        [MBManager showBriefAlert:@"手机号不正确!"];
        return;
    }else{
        
        [self timeCountDown];
        
        [self CodepostUrl];
    }
    
}
- (void)timeCountDown {
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.ForgetView.codeButton.userInteractionEnabled = YES;
                self.ForgetView.codeButton.titleLabel.font = [UIFont systemFontOfSize:Fontfour];
                [self.ForgetView.codeButton setTitle:@"重新接收" forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout % 90;
            NSString *strTime = [NSString stringWithFormat:@"%d秒后重发", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.ForgetView.codeButton.userInteractionEnabled = NO;
                self.ForgetView.codeButton.titleLabel.font = [UIFont systemFontOfSize:Fontfour];
                [self.ForgetView.codeButton setTitle:strTime forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}


- (void)CodepostUrl{
    NSString *iphoneTextStr = [self.ForgetView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSDictionary * params = @{@"phone":iphoneTextStr};
    [HTTPRequest POST:MSsendSms params:params succeed:^(id responseObject) {
//        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [MBManager showBriefAlert:@"发送成功"];
            self.msgId = responseObject[@"data"][@"msgId"];
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];
    
}

/**
 * 新密码可见
 */
- (void)AnimationnewvisibleButtonClick:(UIButton *)sender{
    WYCLogFunc;
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.ForgetView.newpassworldTextField.secureTextEntry = YES;
    }else{
        self.ForgetView.newpassworldTextField.secureTextEntry = NO;
    }
}
/**
 * 注册按钮
 */
- (void)AnimationconfirmvisibleButtonButtonClick:(UIButton *)sender{
    WYCLogFunc;
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.ForgetView.confirmpassworldTextField.secureTextEntry = YES;
    }else{
        self.ForgetView.confirmpassworldTextField.secureTextEntry = NO;
    }
}
@end
